/* ****************************************************************************
 *
 * Copyright (c) Microsoft Corporation. 
 *
 * This source code is subject to terms and conditions of the Microsoft Public License. A 
 * copy of the license can be found in the License.html file at the root of this distribution. If 
 * you cannot locate the  Microsoft Public License, please send an email to 
 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 *
 * You must not remove this notice, or any other, from this software.
 *
 * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
 * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
 * PARTICULAR PURPOSE.
 * ***************************************************************************/


#include "stdafx.h"
#include <pgl/core/PGLObjectPropPage.h>
#include <pgl/core/PGLRegion.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CPGLObjectPropPage property page

IMPLEMENT_DYNCREATE(CPGLObjectPropPage, CPropertyPage)

CPGLObjectPropPage::CPGLObjectPropPage(CPGLObject* _pObject) : CPropertyPage(CPGLObjectPropPage::IDD)
{
	pObject=_pObject;
	//{{AFX_DATA_INIT(CPGLObjectPropPage)
	m_selected = FALSE;
	m_visible = FALSE;
	m_name = _T("");
	m_alpha = 0.0f;
	//}}AFX_DATA_INIT
}

CPGLObjectPropPage::~CPGLObjectPropPage()
{
}

void CPGLObjectPropPage::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPGLObjectPropPage)
	DDX_Check(pDX, IDC_PGL_CHECK_SELECTED, m_selected);
	DDX_Check(pDX, IDC_PGL_CHECK_VISIBLE, m_visible);
	DDX_Text(pDX, IDC_PGL_EDIT_NAME, m_name);
	DDX_Text(pDX, IDC_PGL_EDIT_ALPHA, m_alpha);
	DDV_MinMaxFloat(pDX, m_alpha, 0.f, 1.f);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CPGLObjectPropPage, CPropertyPage)
	//{{AFX_MSG_MAP(CPGLObjectPropPage)
	ON_BN_CLICKED(IDC_PGL_BUTTON_CHANGE_COLOR, OnButtonChangeColor)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPGLObjectPropPage message handlers

void CPGLObjectPropPage::OnButtonChangeColor() 
{
	// let's create the COLORREF
	CColorDialog pColDlg;

	if (pColDlg.DoModal()==IDOK)
	{
		COLORREF col=pColDlg.GetColor();
		pObject->SetColor((float)(((col<<24)>>24)/255.0),
				(float)(((col<<16)>>24)/255.0),
				(float)((col>>16)/255.0),
				pObject->GetColor().GetAlpha());
	}
}

BOOL CPGLObjectPropPage::OnSetActive() 
{
	m_alpha=(pObject->GetColor()).GetAlpha();
	m_name=pObject->GetName();
	m_selected=pObject->IsSelected();
	m_visible=pObject->IsVisible();

	UpdateData(FALSE);	

	return CPropertyPage::OnSetActive();
}

BOOL CPGLObjectPropPage::OnKillActive() 
{
	UpdateData(TRUE);

	pObject->SetName(m_name);
	pObject->GetColor().SetAlpha(m_alpha);

	if (m_visible)
		pObject->Show();
	else
		pObject->Hide();
	
	return CPropertyPage::OnKillActive();
}

